Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
replace-string
Advanced tools
The replace-string npm package is a simple utility for replacing all occurrences of a substring within a string with a new substring. It is useful for string manipulation tasks where you need to perform replacements in a straightforward and efficient manner.
Basic Replacement
This feature allows you to replace all occurrences of a specified substring within a string with a new substring. In this example, 'World' is replaced with 'Universe'.
const replaceString = require('replace-string');
const result = replaceString('Hello World', 'World', 'Universe');
console.log(result); // 'Hello Universe'
Case Sensitive Replacement
The replacement is case-sensitive by default. In this example, 'world' is not replaced because it does not match the case of 'World'.
const replaceString = require('replace-string');
const result = replaceString('Hello World', 'world', 'Universe');
console.log(result); // 'Hello World'
Replacing Special Characters
This feature allows you to replace substrings that include special characters. In this example, '$World$' is replaced with 'Universe'.
const replaceString = require('replace-string');
const result = replaceString('Hello $World$', '$World$', 'Universe');
console.log(result); // 'Hello Universe'
The string-replace-all package provides similar functionality to replace-string, allowing you to replace all occurrences of a substring within a string. It offers a straightforward API and is useful for basic string replacement tasks.
The replace package is a more versatile tool that can be used for replacing strings in files as well as in strings. It supports regular expressions and offers more advanced replacement options compared to replace-string.
The string.prototype.replaceall package is a polyfill for the String.prototype.replaceAll method, which is a standard method in modern JavaScript. It provides a native way to replace all occurrences of a substring within a string, making it a more standardized option compared to replace-string.
Replace all substring matches in a string
Similar to String#replace()
, but supports replacing multiple matches. You could achieve something similar by putting the string in a RegExp
constructor with the global flag and passing it to String#replace()
, but you would then have to first escape the string anyways.
$ npm install replace-string
const replaceString = require('replace-string');
const string = 'My friend has a 🐑. I want a 🐑 too!';
replaceString(string, '🐑', '🦄');
//=> 'My friend has a 🦄. I want a 🦄 too!'
Returns a new string with all needle
matches replaced with replacement
.
Type: string
String to work on.
Type: string
String to match in input
.
Type: string | Function
Replacement for needle
matches.
If a function, it receives the matched substring, the match count, the original input, and the index in which the match happened (as measured from the original input):
replaceString('Foo 🐑 Bar', '🐑', (matchedSubstring, matchCount, input, matchIndex) => `${matchedSubstring}❤️`);
//=> 'Foo 🐑❤️ Bar'
Type: object
Type: number
Default: 0
Index at which to start replacing.
Type: boolean
Default: false
Whether or not substring matching should be case-insensitive.
RegExp
matches in a stringFAQs
Replace all substring matches in a string
The npm package replace-string receives a total of 227,390 weekly downloads. As such, replace-string popularity was classified as popular.
We found that replace-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.